package com.shu.mq;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author ssm
 * @version V1.0
 * @description:
 * @date 2020/9/14 14:19
 */

// 消费者
public class Consumer {
    private static final String EXCHANGE_NAME = "exchange_return_1";
    private static final String ROUTING_KEY = "return.save";
    private static final String QUEUE_NAME = "queue_return_1";

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        channel.exchangeDeclare(EXCHANGE_NAME, "topic");
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者收到消息：" + new String(body));
//                channel.basicAck(envelope.getDeliveryTag(),false);
//                channel.basicReject(envelope.getDeliveryTag(),false);
                channel.basicNack(envelope.getDeliveryTag(), false, true);
            }
        };
        channel.basicConsume(QUEUE_NAME, false, defaultConsumer);
    }
}